﻿using KZ.Express.ENT;
using KZ.Express.Resource;
using KZ.Utilities;
using System.Collections.Generic;
using System.Data;

namespace KZ.Express.Task
{
    public class ZoneTask : TaskBase
    {
        private new ZoneENT _entity;

        public ZoneTask()
            : base()
        {
            base._entity = _entity = new ZoneENT();
            _entitytype = _entity.GetType();
        }

        public DataTable LoadZoneDetail(SearchCriteria search)
        {
            return _dalBase.SelectDataTable<ZoneDetailENT>(search);
        }

        public int Save(ZoneENT zone, List<DataRow> rows)
        {
            base._dalBase.BeginTransaction();

            ValidateData(zone);
            try
            {
                if (zone.ZoneID == 0)
                    zone.ZoneID = base._dalBase.Insert(zone);
                else
                    base._dalBase.Update(zone);

                foreach (DataRow row in rows)
                {
                    ZoneDetailENT zoneDetail = new ZoneDetailENT();
                    zoneDetail.ZoneID = zone.ZoneID;
                    zoneDetail.Zone = Utility.StringParse(row["Zone"]);
                    zoneDetail.CountryID = Utility.IntParse(row["CountryID"]);

                    base._dalBase.Insert(zoneDetail);
                }

                base._dalBase.CommitTransaction();
                return zone.ZoneID;
            }
            catch (DataAccessException ex)
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(ex.Message);
            }
            catch
            {
                base._dalBase.RollbackTransaction();
                throw new BusinessLogicException(Message.BLM_ERR_SAVE.ToString());
            }
        }
    }
}